<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <!-- <role-search @search="reload" /> -->
      <!-- 数据表格 -->

      <ele-pro-table ref="table"
                     :columns="columns"
                     :datasource="datasource"
                     :selection.sync="selection"
                     height="calc(100vh - 265px)"
                     full-height="calc(100vh - 116px)"
                     tool-class="ele-toolbar-form"
                     row-key="id">

        <template v-slot:toolbar>

        </template>

        <template v-slot:messageType="{ row }">
          <el-tag v-if="row.messageType == 1"
                  type="warning">跳转公众号</el-tag>
          <el-tag v-if="row.messageType == 2"
                  type="success">跳转小程序</el-tag>
        </template>
        <template v-slot:appletName="{ row }">
          <div>{{ row.appletName?row.appletName:'--' }}</div>
        </template>
        <template v-slot:notShow="{ row }">
          <el-switch :active-value="1"
                     :inactive-value="2"
                     v-model="row.notShow"
                     @change="editStatus(row)" />
        </template>

        <template v-slot:discount_money="{ row }">
          <div>
            {{ row.discount_money }}
          </div>
        </template>
        <template v-slot:sort="{ row }">
          <i class="sort-handle el-icon-rank ele-text-secondary"
             style="cursor: move"></i>
          <div v-if="false">{{ row }}</div>
        </template>

        <template v-slot:action="{ row }">
          <el-link type="primary"
                   :underline="false"
                   icon="el-icon-edit"
                   @click="copyAddress(row.link)">
            复制
          </el-link>

        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->

  </div>
</template>
  
  <script>

import { moduleList } from '@/api/HomePageSettings/index'



export default {
  name: 'Recharge',
  components: {
    // RoleSearch,
    // RoleEdit
  },

  data () {
    return {

      // 表格列配置
      columns: [
        {
          type: 'index',
          columnKey: 'index',
          width: 48,
          align: 'center',
          showOverflowTooltip: true,

          fixed: 'left'
        },
        {
          prop: 'name',
          label: '菜单名字',
          // sortable: 'custom',

          align: 'center',

          showOverflowTooltip: true
        },


        {
          prop: 'link',
          label: '跳转链接',
          sortable: 'custom',
          showOverflowTooltip: true,

          align: 'center'
        },

        {
          columnKey: 'action',
          label: '操作',

          align: 'center',
          resizable: false,
          slot: 'action',

          showOverflowTooltip: true,
          fixed: 'right'
        }
      ],
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,

      // 是否显示编辑弹窗
      drawerEditBoolen: false,
      // 是否显示导入弹窗
      showAuth: false,
      showChoose: false,
      gzhLLink: []
    };
  },

  mounted () {


  },
  computed: {},

  methods: {
    /* 表格数据源 */
    async datasource ({ page, limit, where, }) {

      const res = await moduleList({
        page,
        limit,
        ...where,
        type: 3

      });
      console.log(res)
      res.list.forEach((item) => {
        item.link = window.location.origin + item.link
      })

      return res
    },


    // 复制链接
    copyAddress (val) {
      var copyInput = document.createElement("input"); // 创建元素
      //val是要复制的内容
      copyInput.setAttribute("value", val);
      document.body.appendChild(copyInput);
      copyInput.select();
      try {
        var copyed = document.execCommand("copy");
        if (copyed) {
          document.body.removeChild(copyInput);

          this.$message.success("复制成功");
        }
      } catch {
        this.$message.error("复制失败，请检查浏览器兼容");
      }
    },

    // async init () {
    //   const res = await accountMenuBarList({
    //     page: 1,
    //     limit: 50,
    //     siteId: this.$store.state.site.info.id
    //   });
    //   console.log(res);
    //   res.list.forEach((item) => {
    //     item.url = window.location.origin + '/' + item.url
    //   })

    //   this.gzhLLink = res.list;
    // },


    /* 刷新表格 */
    reload (where) {
      this.$refs.table.reload({ page: 1, where: where });
    },
    /* 显示编辑 */
    async openEdit (row) {
      console.log(row);
      if (row?.id) {
        const res = await accountMenuBarDetail(row.id);

        this.current = res;
        // 用户余额管理
        this.drawerEditBoolen = !this.drawerEditBoolen;
      } else {
        this.drawerEditBoolen = !this.drawerEditBoolen;
        this.current = null;
      }
    },

    /* 更改状态 */
    editStatus (row) {
      const loading = this.$loading({ lock: true });
      siteCategoriesModify({
        id: row.id,
        notShow: row.notShow,
        siteId: this.$store.state.site.info.id
      })
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          row.status = !row.status ? 1 : 2;
          this.$message.error(e.message);
        });
    },

    /* 显示分配权限 */
    openAuth (row) {
      this.current = row;
      this.showAuth = true;
    },
    /* 删除 */
    remove (row) {
      const loading = this.$loading({ lock: true });
      accountMenuBarDelete(row.id)
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch () {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      console.log(this.selection);
      this.$confirm('确定要删除选中的信息吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          console.log(this.selection.map((d) => d.id));
          siteBatchCategoriesDelete(this.selection.map((d) => d.id))
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => { });
    }
  }
};
  </script>
  
  <style lang="less">
.categoryImages {
  width: 50px;
  height: 50px;
}
</style>
  